using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tank : Creation
{
    public float Runspeed;
    public float deltadistance;
    GameObject enemy;
    public Transform player_transform;
    Transform ttransform;
    public int flip;
    private Rigidbody2D myRigidbody;
    findenemy Findenemy;
    public bool randrun;
    Vector3 randposition;
    int sleeptimes = 300;
    public float deltadistance2;
    public float AttackTime;
    float AttackTimeKeeper;
    public GameObject projectile;
    SkillCivilization civilization;
    bool wisper;
    public GameObject spikeweed;
    public GameObject cube;
    public float dietime = 60f;
    public void Start()
    {
        myRigidbody = GetComponent<Rigidbody2D>();
        ttransform = GetComponent<Transform>();
        flip = 1;
        Findenemy = GetComponentInChildren<findenemy>();
        randrun = false;
        civilization = GameObject.FindWithTag("Player").GetComponentInChildren<SkillCivilization>();
        if (civilization != null&&transform.parent==null)
        {
            wisper = civilization.wisper;
            health = health * (1 + civilization.healthratio);
        }
    }
    //public IEnumerator Stiff()
    //{

    //myRigidbody.Sleep();

    //   yield return new WaitForSeconds(stifftime);
    // myRigidbody.WakeUp();
    //   freeze = 1;
    //   animator.SetBool("stiff", false);
    //}
    public override void TakeDamage(float damage)
    {
        health -= 0.8f*damage;
        if (health <= 0)
        {
            if (wisper)
            {
                if (Random.Range(0f, 1f) < 0.1)
                {
                    Vector3 position = ttransform.position;
                    position.y += 1f;
                    Instantiate(spikeweed, position, Quaternion.identity);
                }
                else
                {
                    Vector3 position = ttransform.position;
                    position.y += 1f;
                    Instantiate(cube, position, Quaternion.identity);
                }
                wisper = false;
            }
            Destroy(gameObject);
        }

        //StartCoroutine(Stiff());
    }
    void Flip()
    {
        Vector3 position = randrun ? randposition : player_transform.position;
        if (flip * (position.x - ttransform.position.x) < 0)
        {

            ttransform.localRotation = Quaternion.Euler(0, 0, 0);
            face = 1;

        }
        else if (flip * (position.x - ttransform.position.x) > 0)
        {
            ttransform.localRotation = Quaternion.Euler(0, 180, 0);
            face = -1;
        }
    }
    void Move()
    {
        Vector3 position = randrun ? randposition : player_transform.position;
        if (position.x + deltadistance < ttransform.position.x)
        {
            Vector2 vel = new Vector2(-Runspeed, myRigidbody.velocity.y);
            myRigidbody.velocity = vel;
        }
        else if (position.x - deltadistance > ttransform.position.x)
        {
            Vector2 vel = new Vector2(Runspeed, myRigidbody.velocity.y);
            myRigidbody.velocity = vel;
        }
        else
        {
            Vector2 vel = new Vector2(0, myRigidbody.velocity.y);
            myRigidbody.velocity = vel;
        }
    }
    void Attack()
    {
        if (player_transform.position.x + deltadistance2 >= ttransform.position.x && player_transform.position.x - deltadistance2 <= ttransform.position.x)
        {
            Vector3 temp = ttransform.position;
            temp.y += 0.3f;
            temp.x -= face * 3f;
            Instantiate(projectile, temp, Quaternion.identity,transform);
            AttackTimeKeeper = AttackTime;
        }
    }
    private void getplayer()
    {
        if (player_transform != null) return;

        if (transform.parent != null)
        {
            if (transform.parent.GetComponent<pylon>().enemy != null)
            {
                randrun = false;
                player_transform = transform.parent.GetComponent<pylon>().enemy.transform;
                return;
            }

        }
        enemy = Findenemy.enemy;
        if (enemy != null)
        {
            randrun = false;
            player_transform = enemy.transform;
            return;
        }
        if (randrun == false)
        {
            randrun = true;
            randposition = new(ttransform.position.x + Random.Range(-15.0f, 15.0f), ttransform.position.y);
            //Debug.Log(randrun);
            sleeptimes = 300;
        }
        if (randposition.x + deltadistance >= transform.position.x && randposition.x - deltadistance <= transform.position.x)
        {
            sleeptimes--;
            if (sleeptimes == 0)
                randrun = false;
        }
    }
    public void Update()
    {
        if (dietime < 0)
        {
            Destroy(gameObject);
        }
        getplayer();
        if (player_transform != null || randrun)
        {
            Move();
            Flip();
        }
        if (AttackTimeKeeper <= 0)
        {
            if (player_transform != null && randrun == false)
            {
                Attack();
            }
        }
        else AttackTimeKeeper -= Time.deltaTime;
    }
}
